package cn.lyf.dao;

import cn.lyf.entity.Blog;
import cn.lyf.entity.Body;
<<<<<<< HEAD
import cn.lyf.entity.Comment;
=======
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Repository
public class BlogDaoImpl implements BlogDao {
    @Autowired
    MongoTemplate mongoTemplate;

<<<<<<< HEAD
    //根据博客id查找该博客评论的集合
    @Override
    public List<Comment> findCommentsById(String blogid) {
        Query query = new Query(Criteria.where("id").is(blogid));
        Blog blog = mongoTemplate.findOne(query, Blog.class);
        return blog.getComments();
    }

=======
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    @Override
    public void add(Blog blog) {
     mongoTemplate.save(blog);
    }

<<<<<<< HEAD
    //根据博客id删除博客
    @Override
    public void delById(String blogid) {
        Query query = new Query(Criteria.where("id").is(blogid));
        mongoTemplate.remove(query,Blog.class);
    }
=======
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd

    //根据博客类别的id查找对应的所有博客
    @Override
    public List<Blog> findBlogsByTypeId(String typeid) {
        Query query = new Query(Criteria.where("blogtypeid").is(typeid));
        query.with(Sort.by(Sort.Order.desc("tcreate")));
        return  mongoTemplate.find(query,Blog.class);

    }

    //根据id查找对应的博客
    @Override
    public Blog findOneBlog(String blogid) {
        Query query = new Query(Criteria.where("id").is(blogid));
        return mongoTemplate.findOne(query,Blog.class);
    }

    //根据标题模糊查询
    @Override
    public List<Blog> findBlogsByTitle(String title) {
<<<<<<< HEAD
        System.out.println("title2=="+title);
        List<Blog> blogs = new ArrayList<>();
        if(title!=null&&!"".equals(title)){
            System.out.println("title not null");
=======
        List<Blog> blogs = new ArrayList<>();
        if(title!=null&&!"".equals(title)){
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
            Criteria c = new Criteria();
            Pattern pattern = Pattern.compile("^.*"+title+".$",Pattern.CASE_INSENSITIVE);
            c.and("title").regex(pattern);
            Query query = new Query(c);
            blogs= mongoTemplate.find(query,Blog.class);
        }
        return blogs;
    }

<<<<<<< HEAD
    //根据用户id获取博客集合
    @Override
    public List<Blog> findBlogsByUid(String uid) {
        List<Blog> blogs = new ArrayList<>();
        if(uid!=null&&!"".equals(uid)){
            Query query =  new Query(Criteria.where("uid").is(uid));
            query.with(Sort.by(Sort.Order.desc("tcreate")));
            blogs = mongoTemplate.find(query, Blog.class);

        }
        return blogs;
    }
=======

>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
}
